import requests

with open("./movies.txt", "w") as fp:
    page_limit = 50
    page_start = 0

    while True:
        # 向目标地址发起GET请求，并返回Response对象
        r = requests.get("https://movie.douban.com/j/search_subjects",
                         params={"type": "movie", "tag": "热门", "page_limit": page_limit, "page_start": page_start},
                         headers={
                             "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"})
        # print(r.status_code)
        # print(r.encoding)
        # print(r.url)
        # r.encoding = "utf8"
        if r.status_code == 200:
            # 将返回的数据进行保存
            movies = r.json()['subjects']
            import json

            print("Requested %d movies" % (len(movies)))
            for m in movies:
                fp.write(json.dumps(m) + "\n")

            # 测试是否是最后一页
            if len(movies) < page_limit:
                break
            else:
                # 访问下一页
                page_start += len(movies)
        else:
            break

        # import json
        #
        # data = json.loads(r.text)
        # for m in data['subjects']:
        #     print(m['title'])

        # print(r.json()["subjects"])
    # else:
    #     print("Error occurred")
